package com.wjjm.spoonselling.service.impl;

import com.wjjm.spoonselling.dao.MerchantDao;
import com.wjjm.spoonselling.pojo.Merchant;
import com.wjjm.spoonselling.service.MerchantService;
import com.wjjm.spoonselling.util.Code;
import com.wjjm.spoonselling.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantDao merchantDao;


    @Override
    public Result login(Merchant merchant) {
        Result result = new Result();
        try{
            Merchant m = merchantDao.queryMerchantByNameAndPwd(merchant);
            if(m == null){
                result.setMsg("用户名或密码错误");
                result.setCode(Code.SELECT_ERR);
            }else {
                result.setMsg("登录成功");
                result.setCode(Code.SELECT_OK);
                merchant.setMerchantId(m.getMerchantId());
                result.setData(merchant);
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @Override
    /**
     * 注册
     */
    public Result register(Merchant merchant) {
        Result result = new Result();
        try {
            Merchant m = merchantDao.queryMerchantByName(merchant.getName());
            if(m != null){
                //该商家已经存在，不允许注册
                result.setMsg("用户名已存在");
                result.setCode(Code.ADD_ERR);
            }else {
                merchantDao.register(merchant);
                result.setMsg("注册成功");
                result.setCode(Code.ADD_OK);
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Merchant selectByName(String name) {
        Merchant merchant = merchantDao.queryMerchantByName(name);
        return merchant;
    }

    @Override
    public boolean update(Merchant merchant) {
        int i = merchantDao.update(merchant);
        return i>0;
    }

}
